package com.ayf.payment.game.api.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.entity.platform.SmsRecord;
import com.ayf.payment.game.api.httpclient.HttpUtils;
import com.ayf.payment.game.api.type.SMSType;
import com.ayf.payment.game.api.utils.RandomStrUtils;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 创蓝
 */
@Component
public class ChuangLanSMSHandler extends SMSHandler {

    public void send(String phone, SMSType type, SmsChannelConfig sMSConfig, Integer merchantId) {

        JSONObject config = JSONObject.parseObject(sMSConfig.getChannelConfig());
        final String url = config.getString("url");
        final String account = config.getString("account");
        final String password = config.getString("password");
        final String smsSign = config.getString("sign");

        String code = RandomStrUtils.createRandom(true, 6);
        String msg = smsSign + MessageFormat.format(type.getDesc(), code);

        Map<String, String> data = new HashMap<>();
        data.put("account", account);
        data.put("password", password);
        data.put("phone", phone);
        data.put("msg", msg);

        String traceId = SnowFlakeUtils.getTraceId();
        try {
            Map<String, String> map = HttpUtils.noSignPost(traceId, url, JSON.toJSONString(data));
            String date = map.get(HttpUtils.RESPONSE_DATA);
            JSONObject jsonDate = JSONObject.parseObject(date);
            SmsRecord smsRecord = new SmsRecord();
            if (map.get(HttpUtils.RESPONSE_CODE).equals("200")) {
                if (jsonDate.getString("code").equals("0")) {
                    smsRecord.setStatus(1);
                    LogPortal.info("[{}]创蓝短信验证码发送成功,发送手机号:[{}],参数:[{}],返回结果:[{}]", traceId, phone, data, map);
                } else {
                    smsRecord.setStatus(0);
                    LogPortal.error("[{}]创蓝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],原因为:[{}]", traceId, phone, data.get("msg"), jsonDate.getString("errorMsg"));
                }
                smsRecord.setSendPhone(phone);
                smsRecord.setSmsChannel(sMSConfig.getChannelName());
                smsRecord.setSendContent(msg);
                if (StringUtils.isNotEmpty(jsonDate.getString("msgId"))) {
                    smsRecord.setChannelMsgid(jsonDate.getString("msgId"));
                }
                if (StringUtils.isNotEmpty(jsonDate.getString("code"))) {
                    smsRecord.setResponseCode(jsonDate.getString("code"));
                }
                if (StringUtils.isNotEmpty(jsonDate.getString("errorMsg"))) {
                    smsRecord.setResponseCodeMsg(jsonDate.getString("errorMsg"));
                }
            } else {
                smsRecord.setStatus(0);
                smsRecord.setResponseCodeMsg("请求创蓝短信接口失败");
                LogPortal.error("[{}]创蓝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],调用接口失败", traceId, phone, data.get("msg"));
            }
            save(smsRecord, type, code, merchantId);
        } catch (IOException e) {
            LogPortal.error("[{}]创蓝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],异常为[{}]", traceId, phone, data.get("msg"), e.getMessage());
        }
    }
}
